<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="商品ID(数字0：未插入)" prop="goodId">
        <el-input v-model="dataForm.goodId" placeholder="商品ID(数字0：未插入)"></el-input>
      </el-form-item>
      <el-form-item label="图片名称(字符串空：未插入)" prop="goodImgName">
        <el-input v-model="dataForm.goodImgName" placeholder="图片名称(字符串空：未插入)"></el-input>
      </el-form-item>
      <el-form-item label="图片路径(字符串空：未插入)" prop="goodImgUrl">
        <el-input v-model="dataForm.goodImgUrl" placeholder="图片路径(字符串空：未插入)"></el-input>
      </el-form-item>
      <el-form-item label="图片大小(字符串空：未插入)" prop="goodImgSize">
        <el-input v-model="dataForm.goodImgSize" placeholder="图片大小(字符串空：未插入)"></el-input>
      </el-form-item>
      <el-form-item label="图片顺序(0：未插入,1.第一个2.第二个)" prop="goodImgOrder">
        <el-input v-model="dataForm.goodImgOrder" placeholder="图片顺序(0：未插入,1.第一个2.第二个)"></el-input>
      </el-form-item>
      <el-form-item label="图片状态(数字0：未插入，1.启用,2：待启用，)" prop="goodImgStatus">
        <el-input v-model="dataForm.goodImgStatus" placeholder="图片状态(数字0：未插入，1.启用,2：待启用，)"></el-input>
      </el-form-item>
      <el-form-item label="商品类型；(数字0：未插入，1.是,2：否，)" prop="goodImgType">
        <el-input v-model="dataForm.goodImgType" placeholder="商品类型；(数字0：未插入，1.是,2：否，)"></el-input>
      </el-form-item>
      <el-form-item label="描述(字符串空：未插入)" prop="goodImgDesc">
        <el-input v-model="dataForm.goodImgDesc" placeholder="描述(字符串空：未插入)"></el-input>
      </el-form-item>
      <el-form-item label="操作员(字符串空：未插入)" prop="operator">
        <el-input v-model="dataForm.operator" placeholder="操作员(字符串空：未插入)"></el-input>
      </el-form-item>
      <el-form-item label="创建即上传时间(数字0：未插入)" prop="createUtc">
        <el-input v-model="dataForm.createUtc" placeholder="创建即上传时间(数字0：未插入)"></el-input>
      </el-form-item>
      <el-form-item label="修改即更新时间(数字0：未插入)" prop="modifyUtc">
        <el-input v-model="dataForm.modifyUtc" placeholder="修改即更新时间(数字0：未插入)"></el-input>
      </el-form-item>
      <el-form-item label="是否删除(数字0：未插入，1.未删除,2：删除，)" prop="isDeleted">
        <el-input v-model="dataForm.isDeleted" placeholder="是否删除(数字0：未插入，1.未删除,2：删除，)"></el-input>
      </el-form-item>
      <el-form-item label="版本号(数字0：未插入)" prop="version">
        <el-input v-model="dataForm.version" placeholder="版本号(数字0：未插入)"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          goodId: '',
          goodImgName: '',
          goodImgUrl: '',
          goodImgSize: '',
          goodImgOrder: '',
          goodImgStatus: '',
          goodImgType: '',
          goodImgDesc: '',
          operator: '',
          createUtc: '',
          modifyUtc: '',
          isDeleted: '',
          version: ''
        },
        dataRule: {
          goodId: [
            { required: true, message: '商品ID(数字0：未插入)不能为空', trigger: 'blur' }
          ],
          goodImgName: [
            { required: true, message: '图片名称(字符串空：未插入)不能为空', trigger: 'blur' }
          ],
          goodImgUrl: [
            { required: true, message: '图片路径(字符串空：未插入)不能为空', trigger: 'blur' }
          ],
          goodImgSize: [
            { required: true, message: '图片大小(字符串空：未插入)不能为空', trigger: 'blur' }
          ],
          goodImgOrder: [
            { required: true, message: '图片顺序(0：未插入,1.第一个2.第二个)不能为空', trigger: 'blur' }
          ],
          goodImgStatus: [
            { required: true, message: '图片状态(数字0：未插入，1.启用,2：待启用，)不能为空', trigger: 'blur' }
          ],
          goodImgType: [
            { required: true, message: '商品类型；(数字0：未插入，1.是,2：否，)不能为空', trigger: 'blur' }
          ],
          goodImgDesc: [
            { required: true, message: '描述(字符串空：未插入)不能为空', trigger: 'blur' }
          ],
          operator: [
            { required: true, message: '操作员(字符串空：未插入)不能为空', trigger: 'blur' }
          ],
          createUtc: [
            { required: true, message: '创建即上传时间(数字0：未插入)不能为空', trigger: 'blur' }
          ],
          modifyUtc: [
            { required: true, message: '修改即更新时间(数字0：未插入)不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除(数字0：未插入，1.未删除,2：删除，)不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号(数字0：未插入)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.upkgoodimg.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.goodId = data.upkgoodimg.goodId
                this.dataForm.goodImgName = data.upkgoodimg.goodImgName
                this.dataForm.goodImgUrl = data.upkgoodimg.goodImgUrl
                this.dataForm.goodImgSize = data.upkgoodimg.goodImgSize
                this.dataForm.goodImgOrder = data.upkgoodimg.goodImgOrder
                this.dataForm.goodImgStatus = data.upkgoodimg.goodImgStatus
                this.dataForm.goodImgType = data.upkgoodimg.goodImgType
                this.dataForm.goodImgDesc = data.upkgoodimg.goodImgDesc
                this.dataForm.operator = data.upkgoodimg.operator
                this.dataForm.createUtc = data.upkgoodimg.createUtc
                this.dataForm.modifyUtc = data.upkgoodimg.modifyUtc
                this.dataForm.isDeleted = data.upkgoodimg.isDeleted
                this.dataForm.version = data.upkgoodimg.version
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'goodId': this.dataForm.goodId,
              'goodImgName': this.dataForm.goodImgName,
              'goodImgUrl': this.dataForm.goodImgUrl,
              'goodImgSize': this.dataForm.goodImgSize,
              'goodImgOrder': this.dataForm.goodImgOrder,
              'goodImgStatus': this.dataForm.goodImgStatus,
              'goodImgType': this.dataForm.goodImgType,
              'goodImgDesc': this.dataForm.goodImgDesc,
              'operator': this.dataForm.operator,
              'createUtc': this.dataForm.createUtc,
              'modifyUtc': this.dataForm.modifyUtc,
              'isDeleted': this.dataForm.isDeleted,
              'version': this.dataForm.version
            }
            var tick = !this.dataForm.id ? API.upkgoodimg.add(params) : API.upkgoodimg.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
